<?php

namespace App\Models\Traits;

use Bouncer;
use App\Models\Area;

trait UserQueryScope {

    public function scopeUid($query, $field='userid') {

        $user = auth()->user();

        $uid = $user->userid;

        $scope = $user->scope;

        if(!$scope) {
            return $query;
        }

        $node = Area::find($scope);

        if($node->isLeaf()) {
            $query->where($field, $uid);
        }
        
        return $query;
    }

}
